/**
 * FileName: com.XXX.cloud.datas.config
 * Author: XXX
 * Date: 2019/7/4 13:36
 * Description: 线程池
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lpwa.common.config.thread;
 
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
 
import java.util.concurrent.*;
 
/**
 * @Description: 线程池配置
 * @ClassName: ThreadExecutorConfig
 * @Author: XXX
 * @Date: 2019/7/4 13:36
 * @Version: 1.0
 */
@Configuration
@EnableAsync
public class ThreadExecutorConfig {
 
    private static final Logger logger = LoggerFactory.getLogger(ThreadExecutorConfig.class);
 
    /**
     * 核心线程数
     */
    private int corePoolSize = Runtime.getRuntime().availableProcessors();
    /**
     * 最大线程数
     */
    private int maxPoolSize = Integer.MAX_VALUE;
    /**
     * 线程销毁时间
     */
    private Long keepAliveTime = 60L;
    /**
     * 任务队列
     */
    private SynchronousQueue synchronousQueue = new SynchronousQueue<>();
    /**
     * 线程名称
     */
    private ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("datas-thread-%d").build();
 
    @Bean
    public ExecutorService myDataThreadPool() {
        logger.info("线程池创建===>开始");
        /**
         * 创建线程池
         * Runtime.getRuntime().availableProcessors()
         */
        ExecutorService threadPool = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS, synchronousQueue, namedThreadFactory);
        logger.info("线程池创建===>结束");
        return threadPool;
    }
}